package com.kai.controller;


import com.kai.Util.EncryptUtil;
import com.kai.entity.OaAdminUser;
import com.kai.entity.ReturnMessage;
import com.kai.mapper.first.UserMapper;
import com.kai.service.ipml.RedisService;
import com.kai.service.UserService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 登录验证
 * Created by kai on 18-7-31.
 */
@RestController
public class LoginController extends HttpServlet {

    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisService redisService;


    private final static  String SESSION_KEY="uses";
    /**
     * 登录
     * @param user
     * @param
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public  ReturnMessage login(@RequestBody OaAdminUser user, HttpServletRequest request){
        ReturnMessage result=new ReturnMessage();
        try {

            List<OaAdminUser> users =userService.getUser(user);

            //redisService.set(request.getSession().getId(),users.get(0).getId(), (long) 1800000);
            if(users != null && users.size()>0){
                String Md5pwd=EncryptUtil.string2MD5(user.getPassword());
                String kaptchaValue = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
                /*HttpSession httpSession =request.getSession();
                String username =(String)httpSession.getAttribute("KAPTCHA_SESSION_KEY");
                System.out.println(username);
                //验证验证码,不区分大小写
                if(user.getVerifyCode()!=null) {
                    if (!username.toLowerCase().equals(user.getVerifyCode())) {
                        result.setCode("400");
                        result.setMessage("验证码不正确");
                        result.setUser(users.get(0));
                        httpSession.removeAttribute("verifyCode");
                    }
                }*/
                //校验密码
                if(Md5pwd.equals(users.get(0).getPassword())){// 设置session
                    request.getSession().setAttribute(SESSION_KEY,users.get(0).getId());
                    result.setCode("200");
                    result.setMessage("登录成功");
                    result.setUser(users.get(0));
                    //httpSession.removeAttribute("verifyCode");
                }else{
                    result.setCode("400");
                    result.setMessage("密码错误");
                    result.setUser(users.get(0));
                }

            }else{
                result.setCode("400");
                result.setMessage("用户不存在");
            }
        } catch (Exception e) {
            result.setCode("404");
            result.setMessage(e.getMessage().toString());
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public ReturnMessage logout(HttpSession session) {
        session.removeAttribute(SESSION_KEY);
        ReturnMessage result=new ReturnMessage();
        result.setCode("200");
        result.setMessage("登录成功");
        return result;
    }

    /**
     * 更改用户头像
     */
    @RequestMapping(value = "sysUserAvatar",method = RequestMethod.POST)
    @ResponseBody
    public ReturnMessage setUserAvatar(@RequestBody Map<String,Object> map){

        ReturnMessage result=new ReturnMessage();
        map.put("userName","admin123456");
        int i=userMapper.modifyUserVatar(map);
        if(i>0){
            result.setCode("200");
            result.setMessage("上传成功");
        }else{
            result.setCode("400");
            result.setMessage("上传失败");
        }
        return result;
    }
}
